import { Component, Input, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { DebuggerService } from '../../../core/debugger/debugger.service';
import { 
  DebugSession, 
  Breakpoint, 
  DebugVariable, 
  StackFrame, 
  WatchExpression 
} from '../../../core/debugger/debug.interface';

@Component({
  standalone: false,
  selector: 'app-debugger-panel',
  templateUrl: './debugger-panel.component.html',
  styleUrls: ['./debugger-panel.component.css']
})
export class DebuggerPanelComponent implements OnInit, OnDestroy {
  @Input() visible: boolean = false;
  @Output() closeRequested = new EventEmitter<void>();

  currentSession: DebugSession | null = null;
  debugOutput: string[] = [];
  watchExpressions: WatchExpression[] = [];
  newWatchExpression: string = '';
  activeTab: string = 'variables';
  
  private subscriptions: Subscription[] = [];

  constructor(private debuggerService: DebuggerService) {}

  ngOnInit() {
    // 订阅调试会话变化
    this.subscriptions.push(
      this.debuggerService.session$.subscribe(session => {
        this.currentSession = session;
      })
    );

    // 订阅调试输出
    this.subscriptions.push(
      this.debuggerService.output$.subscribe(output => {
        this.debugOutput.push(output);
      })
    );

    // 订阅调试事件
    this.subscriptions.push(
      this.debuggerService.events$.subscribe(event => {
        if (event.type === 'output' && event.text) {
          this.debugOutput.push(event.text);
        }
      })
    );
  }

  ngOnDestroy() {
    this.subscriptions.forEach(sub => sub.unsubscribe());
  }

  // 调试控制方法
  continue() {
    this.debuggerService.continue();
  }

  stepOver() {
    this.debuggerService.stepOver();
  }

  stepInto() {
    this.debuggerService.stepInto();
  }

  stepOut() {
    this.debuggerService.stepOut();
  }

  pause() {
    this.debuggerService.pause();
  }

  terminate() {
    this.debuggerService.terminate();
  }

  // 断点管理
  toggleBreakpoint(breakpoint: Breakpoint) {
    this.debuggerService.toggleBreakpoint(breakpoint.id);
  }

  removeBreakpoint(breakpoint: Breakpoint) {
    this.debuggerService.removeBreakpoint(breakpoint.id);
  }

  // 变量展开/收起
  toggleVariable(variable: DebugVariable) {
    variable.expanded = !variable.expanded;
  }

  // Watch表达式管理
  addWatchExpression() {
    if (this.newWatchExpression.trim()) {
      const watchExpr: WatchExpression = {
        id: 'watch-' + Date.now(),
        expression: this.newWatchExpression.trim()
      };
      
      this.watchExpressions.push(watchExpr);
      this.evaluateWatchExpression(watchExpr);
      this.newWatchExpression = '';
    }
  }

  removeWatchExpression(id: string) {
    this.watchExpressions = this.watchExpressions.filter(expr => expr.id !== id);
  }

  async evaluateWatchExpression(watchExpr: WatchExpression) {
    try {
      const result = await this.debuggerService.evaluateExpression(watchExpr.expression);
      watchExpr.value = result;
      watchExpr.error = undefined;
    } catch (error) {
      watchExpr.error = error as string;
      watchExpr.value = undefined;
    }
  }

  // 重新求值所有Watch表达式
  refreshWatchExpressions() {
    this.watchExpressions.forEach(expr => this.evaluateWatchExpression(expr));
  }

  // 清空调试输出
  clearOutput() {
    this.debugOutput = [];
  }

  // 关闭面板
  close() {
    this.closeRequested.emit();
  }

  // 检查是否可以执行调试操作
  get canExecuteDebugAction(): boolean {
    return this.currentSession !== null && this.currentSession.state !== 'terminated';
  }

  get isRunning(): boolean {
    return this.currentSession?.state === 'running';
  }

  get isStopped(): boolean {
    return this.currentSession?.state === 'stopped' || this.currentSession?.state === 'paused';
  }

  // 添加标签页切换方法
  switchTab(tabId: string) {
    this.activeTab = tabId;
  }
} 